package com.yunfei.api.v2.service;
import com.yunfei.api.v2.common.YunHeTongApiContant;
import com.yunfei.api.v2.param.LongTokenParam;
import com.yunfei.api.v2.param.ReserveNoParam;
import com.yunfei.api.v2.param.heTongSign.HeTongSignParam;
import com.yunfei.bo.result.ResultModel;
import com.yunfei.enums.ResultStatus;
import com.yunfei.mapper.aiiidb.HeTongSignInfoMapper;
import com.yunfei.utils.http.JsonParamHttpUtil;
import com.yunfei.utils.string.StringUtil;
import com.yunfei.yunhetong.heTongSignManager.bo.HeTongSignManagerBo;
import com.yunfei.yunhetong.heTongSignManager.entity.HeTongSignManagerEntity;
import com.yunfei.yunhetong.heTongSignManager.service.HeTongSignManagerService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
* @Description: 合同签署
* @Author: zhengyunfei
* @Date: 2019/8/27
* @time:10:52
*/
@Service
public class ContractSignService {
    @Autowired
    private GetLongTokenService getLongTokenService;
    @Autowired
    private HeTongSignManagerService heTongSignManagerService;
    @Autowired
    private HeTongSignInfoMapper heTongSignInfoMapper;
    //合同签署
    public ResultModel sign(HeTongSignParam param){
        ResultModel resultModel=new ResultModel();
        try{
            String signerId=param.getSignerId();
            LongTokenParam longTokenParam=new LongTokenParam();
            if(!StringUtil.isNullOrEmpty(signerId)){
                longTokenParam.setSignerId(signerId);
            }
            String token=getLongTokenService.getLongToken(longTokenParam);
            String url= YunHeTongApiContant.HETONG_SIGN_API;
            JSONObject jsonObject=JSONObject.fromObject(param);
            String jsonParam=jsonObject.toString();
            JSONObject resultJson=JsonParamHttpUtil.post(token,url,jsonParam);
            System.out.println("合同签署返回结果=========="+resultJson);
            resultModel.setData(resultJson);
           if(resultJson.containsKey("code")){
               int code=(Integer)resultJson.get("code");
               Object msg=resultJson.get("msg");
               if(code!=200){
                   resultModel.setCode(code);
                   resultModel.setMessage(msg.toString());
               }else{
                   resultModel.setCode(ResultStatus.SUCCESS.getCode());
                   resultModel.setMessage(ResultStatus.SUCCESS.getMessage());
               }
           }

        }catch (Exception e){
            resultModel.setCode(ResultStatus.ERROR.getCode());
            resultModel.setMessage(ResultStatus.ERROR.getMessage());
            e.printStackTrace();
        }
        return resultModel;
    }
//更新合同签署状态
public ResultModel updateContractSignStatus(HeTongSignManagerEntity param){
    ResultModel resultModel=new ResultModel();
    try{
        heTongSignInfoMapper.updateStatus(param);
        resultModel.setCode(ResultStatus.SUCCESS.getCode());
        resultModel.setMessage(ResultStatus.SUCCESS.getMessage());
    }catch (Exception e){
        resultModel.setCode(ResultStatus.ERROR.getCode());
        resultModel.setMessage(ResultStatus.ERROR.getMessage());
        e.printStackTrace();
    }
    return resultModel;
}

}
